//
//  HDDeclare.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/7.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDDeclare.h"
#import "sys/utsname.h"
#import "SDImageCache.h"

@implementation HDDeclare

#pragma mark
#pragma mark -------- Init Methods --------

+ (id)sharedDeclare
{
    static id sharedDeclare;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedDeclare = [[self alloc] init];
    });
    
    return sharedDeclare;
}

- (id)init
{
    self = [super init];

    if(self){
        _beaconFlag = NO;
        NSString *storeID = [[NSUserDefaults standardUserDefaults] valueForKey:@"userPhoneID"];
        if (storeID.length == 0 || storeID == nil)
            _userPhoneID = @"iOS00";
    }
    
    NSString *webMapPath = [NSString stringWithFormat:@"%@/Resource/WebMap/", kCachePath];
    _nowMuseumID = @"0001";
    [self createMapPath:webMapPath];
    [self clearTmpPics];
    
    return self;
}

+ (BOOL)isValidBeacon:(NSInteger)beaconNum
{
    BOOL isValid = NO;
    if (beaconNum > 0 && beaconNum < 1000)
        isValid = YES;
    
    return isValid;
}

- (void)createMapPath:(NSString *)webMapPath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = NO;
    NSError *isError = nil;
    BOOL existed = [fileManager fileExistsAtPath:webMapPath isDirectory:&isDir];
    if (isDir == YES && existed == YES) {
        [fileManager removeItemAtPath:webMapPath error:&isError];
    }
    
    [fileManager createDirectoryAtPath:webMapPath withIntermediateDirectories:YES attributes:nil error:nil];
}

- (void)clearTmpPics // 清理SDWebImg缓存
{
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];//可有可无
}

- (void)setUserPhoneID:(NSString *)userPhoneID
{
    if (userPhoneID==nil || userPhoneID.length ==0)
        return;
    
    _userPhoneID = userPhoneID;
    [[NSUserDefaults standardUserDefaults] setValue:userPhoneID forKey:@"userPhoneID"];
}

- (NSString*)deviceVersion
{
    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone4S";
    if ([deviceString isEqualToString:@"iPhone5,2"]||[deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone5";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone5S";
    if ([deviceString isEqualToString:@"iPhone7,1"]||[deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone6";
    if ([deviceString isEqualToString:@"iPhone8,1"]||[deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone6s";
    return deviceString;
}

- (NSString*)nowLanguage
{
    return _language;
} // 当前语种，大写

- (Language_Type)languageIndex
{
    Language_Type languageType = kLanguage_Type_Chinese;
    if ([_language isEqualToString:@"ENGLISH"])
    {
        languageType = kLanguage_Type_English;
    }
    
    else if ([_language isEqualToString:@"JAPANESE"])
    {
        languageType = kLanguage_Type_Japanese;
    }
    
    else if ([_language isEqualToString:@"KOREAN"])
    {
        languageType = kLanguage_Type_Korean;
    }
    else if ([_language isEqualToString:@"RUSSIA"])
    {
        languageType = kLanguage_Type_Russia;
    }
    
    return languageType;
}

- (void)userIDRequestAction {
    
    NSString *userID = [[NSUserDefaults standardUserDefaults] valueForKey:@"userPhoneID"];
    if (userID.length > 8) {
        [self setUserPhoneID:userID];
        return;
    }
    
    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Ticket&a=generate_user_login", HTTP_Request_URL_Prefix];
    NSDictionary *parameters = @{@"app_kind": @"IOS"};
    
    [loginObj requestGet:urlPath Parameter:parameters];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSString *loginCode = dataDic[@"code"];
        if (loginCode.integerValue == 0) {
            NSDictionary *loginDic = dataDic[@"data"];
            NSString *userLogin = loginDic[@"user_login"];
            if ([userLogin isKindOfClass:[NSString class]] || userLogin.length > 0) {
                [self setUserPhoneID:userLogin];
            }
        }
        else {
        }
    };
}

@end
